package com.my.ads.lib.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.myadlib.R;
import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;


/**
 * Created by xuanyang on 2018/3/16.
 */

public class JunkCleanResultActivity extends BaseActivity {

    private FrameLayout back;
    private RelativeLayout relative_ad_24, relative_ad_25;
    private RelativeLayout linear_booster;
    private ImageView imgAd;


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_junk_clean_result);

        initViews();
        click();

        try {
            AdSummary.getInstance().showNativeAd(this, relative_ad_25, false);
            AdSummary.getInstance().showBannerAd(relative_ad_24);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void click() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Intent intent = new Intent(JunkCleanResultActivity.this, PhoneStateActivity.class);
                intent.putExtra("type", "clean");
                startActivity(intent);
            }
        });
        linear_booster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Intent intent = new Intent(JunkCleanResultActivity.this, MemoryBoostActivity.class);
                startActivity(intent);
            }
        });
        imgAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdSummary.getInstance().showInterstitialAd(JunkCleanResultActivity.this, "");
                Intent in = new Intent(JunkCleanResultActivity.this, GiftActivity.class);
                startActivity(in);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("33", "33");
        updateAd(1);
    }

    private void updateAd(int a) {
        Log.e("c", "b" + a);
        SharedPreferences sp_1 = getSharedPreferences(Config.NativeAdTarget, Context.MODE_PRIVATE);
        Long show_time = sp_1.getLong(Config.NativeAdTimeShow, 0L);
        // 刷新原生广告
        if (show_time != 0L && ((System.currentTimeMillis() - show_time) > Config.UpdateNativeAd * 1000)) {
            try {
                AdSummary.getInstance().showNativeAd(this, relative_ad_25, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        SharedPreferences sp_3 = getSharedPreferences(Config.NativeAdTarget, Context.MODE_PRIVATE);
        Long show_time_ad_3 = sp_3.getLong(Config.BannerAdTimeShow, 0L);

        if (show_time_ad_3 != 0L && (System.currentTimeMillis() - show_time_ad_3 >= Config.UpdateNativeAd * 1000)) {
            AdSummary.getInstance().showBannerAd(relative_ad_24);
        }
    }


    private void initViews() {
        back = (FrameLayout) findViewById(R.id.back);
        relative_ad_24 = (RelativeLayout) findViewById(R.id.relative_ad_24);
        relative_ad_25 = (RelativeLayout) findViewById(R.id.relative_ad_25);
        linear_booster = (RelativeLayout) findViewById(R.id.linear_booster);
        imgAd = (ImageView) findViewById(R.id.img_ad);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startThirdActivity();
        finish();
    }

    private void startThirdActivity() {
        Intent intent = new Intent(JunkCleanResultActivity.this, PhoneStateActivity.class);
        intent.putExtra("type", "clean");
        startActivity(intent);
    }

}
